<cfcomponent>
	
	<!--- 	Date: 11/9/2007  Usage: I perform initialization functions for this component --->
	<cffunction name="init" output="false" access="public" returntype="CategoryValidator" hint="I perform initialization functions for this component">
		<cfreturn this />		
	</cffunction>

	<!--- validate --->
	<cffunction name="validate" access="public" output="false">
		<cfargument name="Category" required="true" />
		<cfargument name="UserMSG" required="true" />

		<cfset var ResultStruct = StructNew() />
		<cfset var isValid = false />
		
		<!--- ProcessRules --->
		
		<cfset ResultStruct[ validateCategoryID( ArgumentCollection=arguments) ] = "" />
		<cfset ResultStruct[ validateCategoryName( ArgumentCollection=arguments) ] = "" />
		<cfset ResultStruct[ validateCategoryBGColor( ArgumentCollection=arguments) ] = "" />
		<cfset ResultStruct[ validateCategoryTextColor( ArgumentCollection=arguments) ] = "" />
		
		<!--- See if we passed validation --->
		<cfif NOT structKeyExists( ResultStruct, "False")>
			<cfset isValid = true>
		</cfif>
		
		<cfreturn isValid />
	
	</cffunction>
	
	<cffunction name="validateCategoryID" access="public" output="false">
		<cfargument name="Category" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />

		<!--- Category is not required. It is numeric --->
		<cfif (len(trim(Category.getCategoryID())) AND NOT isNumeric(trim(Category.getCategoryID())))>
			<cfset Error="The CategoryID must be numeric" />
		</cfif>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>
	
	<cffunction name="validateCategoryName" access="public" output="false">
		<cfargument name="Category" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />

		<!--- This is a Kalendaring app, so we need a date --->
		<cfif NOT len(trim(Category.getCategoryName())) >
			<cfset Error = "Category Name is required." />
		
		<cfelseif len(trim(Category.getCategoryName())) AND NOT IsSimpleValue(trim(Category.getCategoryName())) >
			<cfset Error = "Category Name must be a string" />
		
		<cfelseif len(trim(Category.getCategoryName())) GT 50>
			<cfset Error = "Category Name must be 50 characters or less" />
		</cfif>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>	

	<cffunction name="validateCategoryBGColor" access="public" output="false">
		<cfargument name="Category" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />

		<cfif len(trim(Category.getCategoryBGColor())) AND NOT IsSimpleValue(trim(Category.getCategoryBGColor())) >
			<cfset Error = "Category Background Color must be a string" />
		
		<cfelseif len(trim(Category.getCategoryBGColor())) GT 7>
			<cfset Error = "Category Background Color must be 50 characters or less" />
		</cfif>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>	

	<cffunction name="validateCategoryTextColor" access="public" output="false">
		<cfargument name="Category" required="true" />
		<cfargument name="UserMSG" required="true" />
		
		<cfset var Error="" />

		<cfif len(trim(Category.getCategoryTextColor())) AND NOT IsSimpleValue(trim(Category.getCategoryTextColor())) >
			<cfset Error = "Category Text Color must be a string" />
		
		<cfelseif len(trim(Category.getCategoryTextColor())) GT 7>
			<cfset Error = "Category Text Color must be 50 characters or less" />
		</cfif>
		
		<cfreturn processError( arguments.UserMSG, Error ) />
	</cffunction>	
		

	<cffunction name="processError" output="false" access="private" returntype="boolean" hint="I add errors to the user defined collection">
		<cfargument name="MsgCollection" type="any" required="true"/>
		<cfargument name="Message" type="string" default="" />
		<cfset var isOK = true />

			<cfif len( trim( arguments.Message ) )>
				<cfset isOK = false />		
				<cfset arguments.MsgCollection.addError( arguments.Message ) />
			</cfif>

		<cfreturn isOK />
		
	</cffunction>

</cfcomponent>